package com.example.xyr.myapplication.ui

import android.graphics.Bitmap
import android.view.View
import cn.jpush.im.android.api.ContactManager
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback
import cn.jpush.im.android.api.model.UserInfo
import cn.jpush.im.api.BasicCallback
import com.example.xyr.myapplication.Constant
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.ui.im.ChatUserUtils
import com.example.xyr.myapplication.utils.BitmapUtils
import com.example.xyr.myapplication.utils.LogUtil
import com.example.xyr.myapplication.utils.MyIntent
import com.example.xyr.myapplication.utils.StringUtil
import com.example.xyr.myapplication.view.DialogDisplay
import com.jrmf360.tools.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_add_friend.*
import kotlinx.android.synthetic.main.titlebar_layout.*


/**
 * Created by ylcf on 2018/8/29.
 */
class FindActivity : BaseActivity(), View.OnClickListener {


    override fun getLayout(): Int = R.layout.activity_add_friend

    private lateinit var otherName: String

    override fun init() {
        titleBar.setTitle("添加好友")
    }

    override fun initListener() {
        titleBar.ivBack.setOnClickListener(this)
        find_bt.setOnClickListener(this)
        find_bt_add.setOnClickListener(this)
    }

    override fun initDate(intent: MyIntent?) {

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
            R.id.find_bt -> findUserInfo()
            R.id.find_bt_add -> {
                if (StringUtil.isNotEmpty(otherName)){
                    if (otherName == BitmapUtils.getInstance().name){
                        ToastUtil.showLongToast(mActivity,"不能添加自己为好友")
                    }else{
                        addUser(otherName)
                    }
                }
            }
        }
    }

    /**
     * 查找好友
     */
    fun findUserInfo() {
        DialogDisplay.getInstance().dialogLoading(this, "查找中。。。")
        var userName = find_et.text.toString().trim()
        if (StringUtil.isNotEmpty(userName)) {
            ChatUserUtils.getUserInfo(userName, object : ChatUserUtils.UserInfoCallback {
                override fun errMsg(err: String?) {
                    DialogDisplay.getInstance().dialogCloseLoading(mActivity)
                    toastWar("未找到该用户")
                }

                override fun gotResult(userInfo: UserInfo?) {
                    DialogDisplay.getInstance().dialogCloseLoading(mActivity)
                    rl_userinfo.visibility = View.VISIBLE
                    userInfo?.getAvatarBitmap(object : GetAvatarBitmapCallback() {
                        override fun gotResult(p0: Int, p1: String?, p2: Bitmap?) {
                            if (p0 == 0) {
                                find_iv_avatar.setImageBitmap(p2)
                            }
                        }
                    })
                    find_tv_name.text = userInfo?.nickname
                    otherName = userInfo?.userName!!
                }
            })
        }
    }

    fun addUser(userId :String){
        ContactManager.sendInvitationRequest(userId, Constant.APPKEY,
                "${BitmapUtils.getInstance().getmNickName()}请求添加你为好友",
                object : BasicCallback() {
            override fun gotResult(responseCode: Int, responseMessage: String) {
                if (0 == responseCode) {
                    toastOk("添加成功")
                    find_bt_add.isClickable = false
                    find_bt_add.setBackgroundDrawable(this@FindActivity.mActivity.resources.getDrawable(R.drawable.shape_no_chick_bt))
                } else {
                    //好友请求发送失败
                    LogUtil.e("tag","添加好友失败")
                }
            }
        })
    }
}